from flask import Flask, render_template
from futu import OpenQuoteContext, Market, RET_OK

app = Flask(__name__)

@app.route('/')
def ipo_list():
    # 连接 Futu OpenD
    quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111)
    
    # 获取港股 IPO 数据
    ret, df = quote_ctx.get_ipo_list(Market.HK)
    quote_ctx.close()

    if ret != RET_OK:
        return f"Error fetching IPO data: {df}", 500

    # 转换成列表字典，方便前端渲染
    ipo_data = df.to_dict('records')
    return render_template('ipo.html', ipo_data=ipo_data)

if __name__ == '__main__':
    app.run(debug=True)
